package jhlee42.swing;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;

import java.awt.Dimension;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JTable;

public class SungJukList extends JFrame {

	private JPanel contentPane;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SungJukList frame = new SungJukList();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SungJukList() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 640	, 480);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setPreferredSize(new Dimension(300, 150));
		panel.add(scrollPane, BorderLayout.NORTH);
		
		
		// db에서 sungjuk 테이블의 테이터를 읽어서
		// Jtable에 출력 
		String DRV = "oracle.jdbc.driver.OracleDriver";
		String URL = "jdbc:oracle:thin:@112.216.253.124:1521:xe";
		String sql ="select*from sungjuk order by sjno desc";
		
		Connection conn = null;
		ArrayList<SungJuk2VO> sjlist = new ArrayList<>();
		
		
		try {
			Class.forName(DRV);
			conn = DriverManager.getConnection(URL,"jhlee183","jhlee183");
			PreparedStatement p = conn.prepareStatement(sql);
			ResultSet rs = p.executeQuery();
			
			while(rs.next()){
				SungJuk2VO sj = new SungJuk2VO(rs.getString(2),rs.getInt(3),rs.getInt(4),rs.getInt(5));
				sjlist.add(sj);
			}
			rs.close(); p.close(); 
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally{
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		// ArrayList에 저장된 성적 데이터들을
		// JTable 모델로 재작성
		
		DefaultTableModel model = new DefaultTableModel();
		// 제목행
		model.setColumnIdentifiers(new String[] {"이름","국어","영어","수학"});
		// 본문행
		for(SungJuk2VO sj : sjlist){
			model.addRow(
					new String[]{sj.getName(),sj.getKor()+"",sj.getEng()+"",sj.getMat()+""}
					);
		}
//		table = new JTable();
//		table.setModel(model);
//		table.setPreferredSize(new Dimension());
//		table.setMinimumSize(new Dimension());
//		table.
		
		
		
		
		table = new JTable();
		table.setModel(model); // 모델 적용
		table.setPreferredSize(new Dimension(300, 150));
		table.setMinimumSize(new Dimension(300, 150));
		scrollPane.setViewportView(table);
	}

}


















